// pages/print/baiduCloud/baiduCloud.js
var utils = require("../../../utils/utils.js");
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        picFixShow: false,
        list: [],
        firstOpen: 0,
        lastPath: '',
        lastName: '',
        key: '',
        pathArray: ['']
    },
    backNav() {
        wx.navigateBack()
    },
    clpi() {
        wx.setClipboardData({
            data: '百度网盘授权',
            success(res) {
                wx.getClipboardData({
                    success(res) {}
                })
            }
        })
    },
    //获取下级目录
    getNextFir(e) {
        var that = this;
        var dir = e.currentTarget.dataset.dir;
        var name = e.currentTarget.dataset.name;
        that.selectComponent("#loading").showLoadingCus('',2);
        utils.request('wxDocs/get', {
            dir: dir
        }, function (res) {
            that.selectComponent("#loading").hideLoadingCus();
            if (res.data.code == 200) {
                if (res.data.isNeedAuth == 1) {
                    that.setData({
                        lastPath: ''
                    })
                } else {
                    var num = 0;
                    var sARRY = []
                    for (var i = 0; i < that.data.pathArray.length; i++) {
                        if (that.data.pathArray[i] == dir) {
                            num++
                        }
                    }
                    if (num == 0) {
                        sARRY.push(dir)
                    }
                    // var lastPath = dir.replace(name, "")
                    // if (lastPath.substring(0, 1) == '/' && lastPath.length > 1) {
                    //     lastPath = lastPath.substring(0, lastPath.length - 1);
                    // } else {
                    //     lastPath = '/' + lastPath.substring(0, lastPath.length - 1);
                    // }
                    that.setData({
                        list: res.data.data,
                        lastName: name,
                        pathArray: that.data.pathArray.concat(sARRY)
                    })
                }
            } else {
                utils.toast(res.data.msg)
            }
        })
    },
    addKey(e) {
        this.setData({
            key: e.detail.value
        })
    },
    //打开授权界面
    openaAuth(appid, path) {
        wx.navigateToMiniProgram({
            appId: appid, //金山文档小程序id
            path: path,
            success(res) {

            }
        })
    },
    //获取用户文件列表
    getIndex() {
        var that = this;
        that.selectComponent("#loading").showLoadingCus('',2);
        utils.request('wxDocs/get', {
            code: app.globalData.wpscode || ''
        }, function (res) {
            that.selectComponent("#loading").hideLoadingCus();
            if (res.data.code == 200) {
                if (res.data.isNeedAuth == 1) {
                    that.setData({
                        lastPath: ''
                    })
                    that.openaAuth(res.data.navUrl.appId, res.data.navUrl.path)
                } else {
                    app.globalData.wpscode = '';
                    that.setData({
                        list: res.data.data,
                        infoUrl: res.data.helpId
                    })
                }
            } else {
                utils.toast(res.data.msg)
            }
        })
    },
    //设置个空白方法，阻止冒泡
    touchAnother() {

    },
    getpdfIntver(id, oid, name) {
        var that = this;
        that.data.inter = setInterval(
            function () {
                utils.request('wxDocs/download', {
                    fileId: id,
                    operationID: oid,
                    sn: wx.getStorageSync('printId'),
                    title: name
                }, function (res) {
                    if (res.data.code == 200 && res.data.isWait != 1) {
                        clearInterval(that.data.inter)
                        if (res.data.isNeedAuth == 1) {
                            that.selectComponent("#loading").hideLoadingCus();
                            that.setData({
                                lastPath: ''
                            })
                        } else {
                            app.globalData.wpscode = '';
                            var reg = /^.*\.(?:xls|xlsx)$/i;
                            if (reg.test(res.data.data.fileName)) {
                                that.excelTpdf(res.data.data.fileName, res.data.data.fileUrl)
                            } else {
                                //非excel
                                //    wx.showLoading()
                                that.kUploadFile(res.data.data.fileName, res.data.data.fileUrl)
                            }
                            // if (Array.isArray(res.data.data)) {
                            //     wx.navigateTo({
                            //         url: '/pages/print/chooseSheet/chooseSheet?list=' + JSON.stringify(res.data.data) + '&code=16968980',
                            //     })
                            // } else {
                            //     var xsk = res.data.data.fileUrl.split('.');
                            //     if (xsk[xsk.length - 1] == 'xlsx' || xsk[xsk.length - 1] == 'xls') {
                            //         var array = [];
                            //         res.data.data.downUrl = res.data.data.fileUrl;
                            //         res.data.data.sheetName = res.data.data.filename.replace(/.xlsx/, "").replace(/.xls/, "");
                            //         array.push(res.data.data)
                            //         wx.navigateTo({
                            //             url: '/pages/print/chooseSheet/chooseSheet?list=' + JSON.stringify(array) + '&code=16968980',
                            //         })
                            //     } else {
                            //         wx.navigateTo({
                            //             url: '../list/list?code=16968980&name=' + res.data.data.filename + '&icon=' + res.data.data.fileIcon + '&path=' + res.data.data.fileUrl + '&pageNum=' + res.data.data.pageNum,
                            //         })
                            //     }
                            // }
                        }
                    }
                    if (res.data.code == -2) {
                        that.selectComponent("#loading").hideLoadingCus();
                        utils.toast(res.data.msg)
                        clearInterval(that.data.inter)
                    }
                })
            }, 3000)
    },
    seejx(e) {
        var url = e.currentTarget.dataset.url;
        wx.navigateTo({
            url: '/pages/my/manualDetail/manualDetail?id=' + url + '&name=如何打印好友分享的文档',
        })
    },
    chooseIndex(e) {
        var id = e.currentTarget.dataset.id;
        var name = e.currentTarget.dataset.name;
        var type = e.currentTarget.dataset.type;
        var chooseIndex = this.data.chooseIndex
        this.setData({
            chooseIndex: chooseIndex == id ? '' : id,
            chooseName: chooseIndex == id ? '' : name,
            chooseType: chooseIndex == id ? '' : type,
        })
    },
    //点击文件获取链接下载
    printGo(e) {
        var that = this;
        var id = that.data.chooseIndex;
        if (!utils.is_define(id)) {
            utils.toast('请先选择要打印的文件哦')
            return false;
        }
        var name = that.data.chooseName;
        var type = that.data.chooseType;
        that.selectComponent("#loading").showLoadingCus('',2);
        utils.request('wxDocs/get', {
            fileId: id,
            type: type,
            sn: wx.getStorageSync('printId'),
            title: name,
            code: app.globalData.wpscode || ''
        }, function (res) {
            if (res.data.code == 200) {
                if (utils.is_define(res.data.operationID)) {
                    //轮询获取文档
                    that.getpdfIntver(id, res.data.operationID, name);
                    return false;
                }
                that.selectComponent("#loading").hideLoadingCus();
                if (res.data.isNeedAuth == 1) {
                    that.setData({
                        lastPath: ''
                    })
                } else {
                    app.globalData.wpscode = '';

                }
            } else {
                that.selectComponent("#loading").hideLoadingCus();
                utils.toast(res.data.msg)
            }
        })
    },
    //excel转pdf
    excelTpdf(path, name) {
        var that = this;
        utils.getSheetByUrl(path, name, function (res) {
            var data = res.data.data
            //    wx.hideLoading()
            that.selectComponent("#loading").hideLoadingCus();
            wx.navigateTo({
                url: '/pages/print/chooseSheet/chooseSheet?list=' + escape(JSON.stringify(data)) + '&serviceMainId=' + that.data.serviceMainId + '&serviceSubtypeId=' + that.data.serviceSubtypeId + '&tagFloor=' + that.data.tagFloor + '&code=' + that.data.code,
            })
        })
    },
    kUploadFile(name, fileUrl) {
        var that = this;
        utils.uploadfile('', name, fileUrl, '', '', function (res) {
            var data = res.data.data
            that.selectComponent("#loading").hideLoadingCus();
            wx.navigateTo({
                url: '../list/list?code=' + that.data.code + '&name=' + data.localName + '&path=' + data.printUrl + '&pageNum=' + data.pageTotal + '&fileUrl=' + data.fileUrl + '&serviceMainId=' + that.data.serviceMainId + '&serviceSubtypeId=' + that.data.serviceSubtypeId + '&tagFloor=' + that.data.tagFloor + '&orientation=' + data.orientation
            })
        })
    },
    // 返回上一层
    goFirst() {
        var that = this;
        var paths = that.data.pathArray;
        paths.pop()
        that.selectComponent("#loading").showLoadingCus('',2);
        utils.request('wxDocs/get', {
            dir: paths[paths.length - 1],
            code: app.globalData.wpscode || ''
        }, function (res) {
            that.selectComponent("#loading").hideLoadingCus();
            if (res.data.code == 200) {
                if (res.data.isNeedAuth == 1) {
                    that.setData({
                        lastPath: ''
                    })
                } else {
                    // app.globalData.wpscode = '';
                    // if (dir == '') {
                    //     dir = ''
                    // } else {
                    //     var newStr = dir.split("/")
                    //     dir = dir.replace('/' + newStr[newStr.length - 1], "")
                    //     if (dir == '') {
                    //         dir = '/'
                    //     }
                    // }
                    that.setData({
                        list: res.data.data,
                        pathArray: paths
                    })
                }
            } else {
                utils.toast(res.data.msg)
            }
        })
    },
    //文档处理
    settingDoc(path, name, icon, pageNum) {
        // utils.uploadfile(path, name, function (res) {
        //   var data = JSON.parse(res.data)
        //   wx.hideLoading()
        //   if (data.code == 1) {
        //     wx.redirectTo({
        //       url: '../list/list?code=' + code + '&name=' + data.name + '&icon=' + data.fileIcon + '&path=' + data.url + '&pageNum=' + data.pageNum,
        //     })
        //   } else {
        //     utils.toast(res.data.msg);
        //   }
        // })
    },
    hidePicFix() {
        this.setData({})
    },
    // 金山授权
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.getIndex();
        wx.getSystemInfo({
            success: (result) => {
                var model = result.model;
                if (model == 'iPhone X') {
                    that.setData({
                        pdbottom: '68',
                        serviceMainId: options.serviceMainId,
                        code: options.code,
                        serviceSubtypeId: options.serviceSubtypeId,
                        tagFloor: options.tagFloor
                    })
                } else {
                    that.setData({
                        pdbottom: '40',
                        serviceMainId: options.serviceMainId,
                        code: options.code,
                        serviceSubtypeId: options.serviceSubtypeId,
                        tagFloor: options.tagFloor
                    })
                }
            },
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        if (that.data.firstOpen == 1) {
            that.au
            this.getIndex();
        } else {
            that.setData({
                firstOpen: 1
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})